package com.dp.service.impl;

import java.util.List;

import javax.persistence.EntityNotFoundException;
import javax.persistence.Query;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dp.service.impl3.BaseImpl;
import com.dp.util.DataMap;
import com.dp.vo.NewsBX;

@Service
@Transactional
public class NewsBXServiceImpl extends BaseImpl {
	
	/**
	 * Find all the NewsBX Entity
	 * @param hashMap
	 * @param firstrow
	 * @param maxrow
	 * @return List<NewsBX>
	 */
	public List<NewsBX> findAllUrlMappingEntity(DataMap hashMap, Integer firstrow,
			Integer maxrow){
		NewsBX urlMapping=(NewsBX) hashMap.get("newsBX");
		List<NewsBX> queryList=null;
		try {
		StringBuffer sf = new StringBuffer("SELECT n FROM NewsBX n WHERE 1=1 ");
		if(urlMapping!=null){
			if(urlMapping.getUrlB()!=null&& urlMapping.getUrlB().toString().trim().length() > 0){
				sf.append(" AND n.urlB like '%"
						+ urlMapping.getUrlB().toString().trim() + "%'");
			}
			if(urlMapping.getUrlX()!=null&& urlMapping.getUrlX().toString().trim().length() > 0){
				sf.append(" AND n.urlX like '%"
						+ urlMapping.getUrlX().toString().trim() + "%'");
			}
		}
		
		
			Query query = em.createQuery(sf.toString());
			if (firstrow != null) {
				query.setFirstResult(firstrow);
			}
			if (maxrow != null) {
				query.setMaxResults(maxrow);
			}
			queryList = query.getResultList();
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}
		return queryList;
	}

	public NewsBX findNewsBXByUrlB(String urlB) {
		NewsBX o = null;
		try {
			o = this.em.find(NewsBX.class, urlB);
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		if (o == null) {
			String msg = "Uh oh, '" + NewsBX.class + "' object with id '" + urlB
					+ "' not found...";
			logger.warn(msg);
			throw new EntityNotFoundException(msg);
		}
	
		return o;
	}

	/**
	 * Update NewsBX Entity
	 * @param newsBX
	 * @return NewsBX
	 */
	public NewsBX updateNewsBX(NewsBX newsBX) {
		try {
			String jpql="UPDATE NewsBX n SET n.urlX=:urlX WHERE n.urlB=:urlB";
			Query query = em.createQuery(jpql);
			query.setParameter("urlX", newsBX.getUrlX());
			query.setParameter("urlB", newsBX.getUrlB());
			int iCode=query.executeUpdate();
			if(iCode<0){
				return null;
			}else{
				newsBX=em.find(NewsBX.class, newsBX.getUrlB());
			}
		} catch (Exception e) {
			logger.warn(e.getMessage());
		}
		return newsBX;
	}

	/**
	 * Delete NewsBX Entity
	 * @param urlB
	 * @return boolean b
	 */
	public boolean deleteNewsBXByUrlB(String urlB) {
		boolean b = false;
		try {
			String jpql="DELETE NewsBX n WHERE n.urlB=:urlB";
			Query query = em.createQuery(jpql);
			query.setParameter("urlB", urlB);
			int iCode=query.executeUpdate();
			if(iCode<0){
				b = false;
			}else{
				b = true;
			}
		} catch (Exception e) {
			logger.warn("deleteUrlMapping fail !!!" + e.getMessage());
		} 
		return b;
	}

	public NewsBX addNewsBX(NewsBX newsBX) {
		try {
			newsBX=em.merge(newsBX);
		} catch (Exception e) {
			logger.info("addNewsBX fail !!!" + e.getMessage());
			newsBX=null;
		} 
		return newsBX;
	}

	public int checkUrlB(String urlB) {
		int iCode=0;
		try {
			StringBuffer sf = new StringBuffer("SELECT n FROM NewsBX n WHERE n.urlB=:urlB ");
			Query query = em.createQuery(sf.toString());
			query.setParameter("urlB", urlB);
			if(query.getResultList().size()>0){
				iCode=1;
			}
		} catch (Exception e) {
			logger.warn(e.getMessage());
		}
		return iCode;
	}
	
}
